package com.edu.juc.lock;

import java.util.concurrent.CompletableFuture;

public class StaticTest {
    static int i = 0;
    int j = 0;

    synchronized static void add() {
        System.out.println("add 开始");
        for (int j = 0; j < 1000; j++) {
            i++;
        }
        System.out.println("add 结束");
    }

    public synchronized void add2() {
        System.out.println("add2 开始");
        for (int j = 0; j < 1000; j++) {
            this.j++;
        }
        System.out.println("add2 结束");
    }

    public void add3() {
        synchronized (Object.class) {
            System.out.println("add3 开始");
            for (int k = 0; k < 1000; k++) {
                i++;
            }
            System.out.println("add3 结束");
        }
    }

    public static void main(String[] args) {
        StaticTest test = new StaticTest();
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(test::add2);
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(StaticTest::add);

        CompletableFuture<Void> future = future1.applyToEither(future2, f -> {
            System.out.println(i);
            return f;
        });
        System.out.println(future.join());

    }
}
